﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IChessEngine;
using System.Drawing;

namespace ChessEngine.PositionTree
{
    //se entiende que no puede haber mas de un movimiento que apunte a una misma posición aunque
    //en rigor esto si podria ocurrir.
    public class ChsMovementRepository : IChsMovementRepository
    {
        private Dictionary<Guid, IChsGameMovement> repository = null;
        private Dictionary<Guid, List<IChsGameMovement>> sourceIndex = null;
        public ChsMovementRepository()
        {
            repository = new Dictionary<Guid, IChsGameMovement>();
            sourceIndex = new Dictionary<Guid, List<IChsGameMovement>>();
        }
        public void Add(IChsGameMovement movement)
        {
            repository[movement.TargetPositionId] = movement;
            if (!(sourceIndex.ContainsKey(movement.SourcePositionId)))
                sourceIndex[movement.SourcePositionId] = new List<IChsGameMovement>();
            sourceIndex[movement.SourcePositionId].Insert(0,movement);
        }
        public IChsGameMovement FindMovementByTarget(Guid guid)
        {
            if (!(repository.ContainsKey(guid)))
                return null;
            else
                return repository[guid];
        }
        public List<IChsGameMovement> FindMovementsBySource(Guid guid)
        {
            if (sourceIndex.ContainsKey(guid))
                return sourceIndex[guid];
            else
                return new List<IChsGameMovement>();
        }
        public List<IChsGameMovement> Movements
        {
            get
            {
                return repository.Values.ToList();
            }
        }
    }
}
